package me.goldze.mvvmhabit.net;

import android.content.Context;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import me.goldze.mvvmhabit.base.Constants;
import me.goldze.mvvmhabit.utils.SPUtils;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class BaseInterceptor implements Interceptor {
    Context mContext;

    public BaseInterceptor(Context context) {
        mContext = context;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Request.Builder builder = chain.request()
                .newBuilder();
        builder.addHeader("Authorization", SPUtils.getInstance().getString(Constants.TOKEN));
        return chain.proceed(builder.build());

    }

    private Map<Object, Object> getHttpUrlParams(FormBody formBody) {
        Map<Object, Object> paramMap = new HashMap<>();
        if (formBody != null) {
            for (int i = 0; i < formBody.size(); i++) {
                paramMap.put(formBody.name(i), formBody.value(i));
            }
        }
        return paramMap;
    }

    private Map<Object, Object> getHttpUrlParams(HttpUrl httpUrl) {
        Set<String> paramsSet = httpUrl.queryParameterNames();
        Map<Object, Object> paramMap = new HashMap<>();
        if (paramsSet != null && paramsSet.size() > 0) {
            for (String s : paramsSet) {
                paramMap.put(s, httpUrl.queryParameter(s));
            }
        }
        return paramMap;
    }




}